//
//  PictureDetailTableViewController.m
//  bh
//
//  Created by lanou on 15/12/22.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "PictureDetailTableViewController.h"
#import "ThirdTableViewCell.h"
#import "PictureDetailTableViewCell.h"
#import "FourTableViewCell.h"
#import "SecondDetailTableViewController.h"
#import "TapAuthorImageTableViewController.h"


@interface PictureDetailTableViewController ()

@property (nonatomic,strong)NSMutableArray *pictureArray;

@property (nonatomic,strong)NSMutableArray *picArray;




@end

static NSInteger indexPathRow = 0;
@implementation PictureDetailTableViewController


- (void)viewWillAppear:(BOOL)animated{

    [self.navigationController setNavigationBarHidden:NO];
}



- (NSMutableArray *)pictureArray{
    if (_pictureArray == nil) {
        _pictureArray = [[NSMutableArray alloc]init];
    }
    return _pictureArray;
}

- (NSMutableArray *)picArray{
    if (_picArray == nil) {
        _picArray = [[NSMutableArray alloc]init];
    }
    return _picArray;
}



- (void)jsonWithPictureDetail{
    
    NSString *url = [NSString stringWithFormat:kSecondPictureDetailUrl,self.picture.photo_id];
    
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *comments = dic[@"data"][@"photo_comments"];
        
        for (NSDictionary *dict in comments[@"comments"]) {
            
            Picture *picture = [Picture shareWithDictionary:dict];
            [self.pictureArray addObject:picture];
        }
        NSDictionary *history = dic[@"data"][@"photo_col_history"];
        for (NSDictionary *list in history[@"list"]) {
            
            Picture *picture1 = [Picture shareWithDictionary:list];
            [self.picArray addObject:picture1];
            
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        
    }];
 
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSLog(@"%@",self.picture.photo_id);
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"editer_ButtonReturn@2x.png"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backLastVC)];
    
    self.navigationController.navigationBar.alpha = 1;

    
    
    self.navigationItem.title = [NSString stringWithFormat:@"来自 %@ 的晒家画册",self.picture.author_nick];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"更多图片" style:(UIBarButtonItemStylePlain) target:self action:@selector(moreDetailTheAuthor)];
    
    
    
    UIImageView *pictureView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/ 2)];
    [pictureView setImageWithURL:[NSURL URLWithString:self.picture.pic_detail]];
    self.tableView.tableHeaderView = pictureView;
    
    [self jsonWithPictureDetail];
    
    [self.tableView registerClass:[ThirdTableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerClass:[PictureDetailTableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

//更多图片的点击事件
- (void)moreDetailTheAuthor{
    SecondDetailTableViewController *SecondDTVC  = [[SecondDetailTableViewController alloc]initWithStyle:(UITableViewStylePlain)];
   
    SecondDTVC.string= self.picture.col_id;
    SecondDTVC.picString = self.picture.pic_detail;
    
    [self.navigationController pushViewController:SecondDTVC animated:YES];
    
}

//返回到上一个界面
- (void)backLastVC{
    [self dismissViewControllerAnimated:NO completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    if (section == 0) {
      return [self.pictureArray count];
    }else if(section == 1){
        return [self.picArray count];
    }else{
        return 1;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ThirdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell.picture = self.pictureArray[indexPath.row];
        // Configure the cell...
        
        return cell;
    }else if (indexPath.section == 1){
        PictureDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell.picture = self.picArray[indexPath.row];
        
        NSLog(@"%@",self.picArray);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tureToOtherView:)];;
        cell.userInteractionEnabled = YES;
        [cell.authorFaceView addGestureRecognizer:tap];
        cell.authorFaceView.tag = indexPath.row;
        return cell;
    }else{
        static NSString *identifier = @"cell3";
        FourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[FourTableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
        }
        return cell;
        
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SecondDetailTableViewController *secondSTVC = [[SecondDetailTableViewController alloc]initWithStyle:(UITableViewStylePlain)];
    Picture *pic = self.picArray[indexPath.row];
       secondSTVC.string =pic.col_id;
    secondSTVC.picString = pic.col_cover;
    [self.navigationController pushViewController:secondSTVC animated:YES];
    
}



- (void)tureToOtherView:(UITapGestureRecognizer *)sender{
    TapAuthorImageTableViewController *tapAuthorITVC = [[[TapAuthorImageTableViewController alloc]init]initWithStyle:(UITableViewStylePlain)];
    Picture *picture = self.picArray[sender.view.tag];
        tapAuthorITVC.authorID = picture.author_id;
    [self.navigationController pushViewController:tapAuthorITVC animated:NO];
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
         UIView *firstView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, kHeight))];
    UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
    label.text = @"评论";
    label.textColor = [UIColor orangeColor];
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(kWidth - 75, 0, 50, 30);
    [button setTitle:@"更多" forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
    firstView.backgroundColor = [UIColor cyanColor];
    [firstView addSubview:button];
    [firstView addSubview:label];
    return firstView;
    }else if (section == 1){
        UIView *secondView =[[UIView alloc]initWithFrame:(CGRectMake(0, 0, kWidth, kHeight))];
        UILabel *label = [[UILabel alloc]initWithFrame:(CGRectMake(5, 0, 100, 30))];
        label.text = @"收入画册";
        label.textColor = [UIColor orangeColor];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
        button.frame = CGRectMake(kWidth - 75, 0, 50, 30);
        [button setTitle:@"更多" forState:(UIControlStateNormal)];
        [button addTarget:self action:@selector(turnToOtherLikePeople) forControlEvents:(UIControlEventTouchUpInside)];
        secondView.backgroundColor = [UIColor cyanColor];
        [secondView addSubview:button];
        [secondView addSubview:label];
        return secondView;
        
        
    }else{
        return nil;
    }
}


- (void)turnToOtherLikePeople{
    NSLog(@"dina45545");
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
